package controller

import (
	"gin/response"
	"gin/service"
	"gin/types"
	"github.com/gin-gonic/gin"
)

var (
	ScheduleInfoService service.ScheduleInfo
)

type ScheduleInfo struct {
}

// Count 交付数量
func (S *ScheduleInfo) Count(ctx *gin.Context) {

	var req types.CountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	resData, queryErr := ScheduleInfoService.Count(tenantId, userId, req.ProjectId, req.IsAll, req.StepCategoryId, req.Month)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// DeliverList 交付数量
func (S *ScheduleInfo) DeliverList(ctx *gin.Context) {

	var req types.DeliverReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")

	resData, queryErr := ScheduleInfoService.DeliverList(req.StepCategoryId, req.Month, req.ProjectId, userId, tenantId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// StepCategoryDevotedList 环节投入
func (S *ScheduleInfo) StepCategoryDevotedList(ctx *gin.Context) {

	var req types.StepCategoryDevotedReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")

	resData, queryErr := ScheduleInfoService.StepCategoryDevotedList(req, userId, tenantId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

//ScheduleTaskCount 排期任务统计
func (S *ScheduleInfo) ScheduleTaskCount(ctx *gin.Context) {
	var req types.ScheduleTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	resData, queryErr := ScheduleInfoService.ScheduleTaskCount(tenantId, userId, req.CurrentMonthDeliver, req.StepCategoryId, req.TaskStatus, req.MonthTime, req.Category, req.Grade, req.DepartmentId, req.Executor, req.ExecutorTenantId)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

//ScheduleStatusTaskCount 排期状态任务统计
func (S *ScheduleInfo) ScheduleStatusTaskCount(ctx *gin.Context) {
	var req types.ScheduleStatusTaskCountReq
	var resData []types.ScheduleStatusTaskCountRes
	var queryErr error
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	if req.StandardCase == "standard" {
		resData, queryErr = ScheduleInfoService.ScheduleStatusTaskCount(req.TaskStatus, req.StatusFilter, tenantId, userId, req.CurrentMonthDeliver, req.StepCategoryId, req.MonthTime, req.Category, req.Grade, req.DepartmentId, req.Executor, req.ExecutorTenantId)
	} else if req.StandardCase == "not_standard" {
		resData, queryErr = ScheduleInfoService.ScheduleNonStandardProjectTaskCount(tenantId, userId, req.TaskStatus, req.MonthTime, req.CurrentMonthDeliver, req.StepCategoryId)
	}
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// ScheduleNonStandardProjectTaskCompletionCount 非标准项目排期任务查询
func (S *ScheduleInfo) ScheduleNonStandardProjectTaskCompletionCount(ctx *gin.Context) {
	var req types.ScheduleNonStandardProjectTaskCompletionCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	resData, queryErr := ScheduleInfoService.ScheduleNonStandardProjectTaskCompletionCount(tenantId, userId, req.ProjectId, req.IsAll, req.StepCategoryId, req.CurrentTime)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// ScheduleNonStandardTaskMemberCount 非标准项目排期任务待办成员统计
func (S *ScheduleInfo) ScheduleNonStandardTaskMemberCount(ctx *gin.Context) {
	var req types.ScheduleNonStandardTaskMemberCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	resData, queryErr := ScheduleInfoService.ScheduleNonStandardTaskMemberCount(tenantId, userId, req.IsAll, req.ProjectId, req.StepCategoryId, req.Page)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// ScheduleNonStandardTaskList 根据用户ID查询非标准项目任务待办
func (S *ScheduleInfo) ScheduleNonStandardTaskList(ctx *gin.Context) {
	var req types.ScheduleNonStandardTaskListReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	resData, queryErr := ScheduleInfoService.ScheduleNonStandardTaskList(req.CurrentMonthDeliver, req.Completion, req.ProjectId, req.StepCategoryId, req.CurrentTime, req.Page)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// NonStandardStepCategoryDevotedList 非标准本月环节已投入
func (S *ScheduleInfo) NonStandardStepCategoryDevotedList(ctx *gin.Context) {
	var req types.StepCategoryDevotedReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	userId := service.GetInt64FromContext(ctx, "user_id")
	resData, queryErr := ScheduleInfoService.NonStandardStepCategoryDevotedList(tenantId, userId, req)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}
